package cn.yunyichina.provider.base.service.impl;

import cn.yunyichina.provider.base.dao.*;
import cn.yunyichina.provider.base.entity.Area;
import cn.yunyichina.provider.base.entity.OrgAppDevelop;
import cn.yunyichina.provider.base.service.OrgService;
import cn.yunyichina.provider.framework.common.PKGenerator;
import cn.yunyichina.provider.framework.dubbo.service.BaseService;
import cn.yunyichina.provider.framework.page.PageInfo;
import cn.yunyichina.provider.iface.entity.base.Org;
import cn.yunyichina.provider.iface.entity.base.OrgApp;
import cn.yunyichina.provider.iface.entity.base.OrgSettingVo;
import cn.yunyichina.provider.iface.entity.base.OrgSettingVoResponse;
import cn.yunyichina.provider.iface.entity.common.ResCommon;
import cn.yunyichina.provider.iface.entity.common.Response;
import cn.yunyichina.utils.convert.JsonUtils;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by CJun on 2016/11/24.
 */
@Service
public class AreaServiceImpl {

    @Autowired
    private AreaDao areaDao;

    private static Logger log = LoggerFactory.getLogger(AreaServiceImpl.class);


    /**
     * 地区信息列表查询
     * @param data(当page_num和page_size为空时表示不分页返回所有数据)
     * @return
     * @author CJun
     */
    public Response listArea(String data) {
        //定义返回结果
        Response response = new Response();
        Map<String, Object> result = new HashMap<>();

        //定义返回列表和分页信息
        List<Area> list = null;
        PageInfo pageInfo = new PageInfo();

        //定义返回码和返回信息
        String resultCode = "1000";
        String resultMessage = "执行成功";

        try {
            //获取入参
            log.info("【地区信息列表查询】开始, 入参->"+data);
            Area areaVo = JsonUtils.parseObject(data, Area.class);
            Map<String, String> maps = JsonUtils.parseObject(data, Map.class);
            String pageNum = maps.get("page_num");
            String pageSize = maps.get("page_size");

            //查询，如需分页则计算分页信息
            if(StringUtils.isNotBlank(pageNum) && StringUtils.isNotBlank(pageSize)){
                long count = areaDao.selectCount(areaVo);
                pageInfo.setTotal(count);
                pageInfo.setPageNum(Integer.valueOf(pageNum));
                pageInfo.setPageSize(Integer.valueOf(pageSize));
                pageInfo.setPages((pageInfo.getTotal().intValue() + pageInfo.getPageSize() - 1) / pageInfo.getPageSize());
                list = areaDao.select(areaVo , pageInfo);
            }else if(StringUtils.isBlank(pageNum) && StringUtils.isBlank(pageSize)){
                list = areaDao.select(areaVo);
            }else{
                resultCode = "1001";
                resultMessage = "参数有误:" + "page_num不能为空或者page_size不能为空";
            }
        } catch (Exception e) {
            resultCode = "1999";
            resultMessage = "系统异常:" + e.getMessage();
            e.printStackTrace();
        } finally {
            //组装返回结果
            result.put("list" , list);
            result.put("page_info" , pageInfo);
            response.setResult(JsonUtils.toJsonString(result));
            response.setResultCodeAndMessage(resultCode, resultMessage);
            log.info("【地区信息列表查询】结束, " + resultCode + ", " + resultMessage);
            return response;
        }
    }
}
